Using RegularExpressionValidator server control, you can check a user's input based on a pattern that you define using a regular expression. This means that you can define a structure that a user's input will be applied against to see if its structure matches the one that you define.
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator">
</asp:RegularExpressionValidator>
The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern.
The validation will not fail if the input control is empty. Use the RequiredFieldValidator control to make the field required.
Properties:
ControlToValidate | The id of the control to validate |
Error Message | The text to display in the page when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set |
Validation Expression | Specifies the expression used to validate input control. |
Code:
<asp:Label ID="Label1" runat="server" Text="email id"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="enter a valid email id" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Submit" />
When we enter invalid email id then error message will show
When we enter valid email id then page will execute
Leave Comment